﻿<documents created="2010-12-15 11:43:24Z"><document src="C:\Users\Anders\Documents\automatones\Visual Studio\AutomaTones\AutomaTones\Model\Tools\Convertor.cs" renderPrefix="s0"><chunk hits="-1" domain="unknown"><source><![CDATA[using System.Diagnostics.Contracts;
using AutomaTones.Model.Automaton_Model;
using Microsoft.Pex.Framework;

namespace AutomaTones.Model.Tools
{
    /// <summary>
    /// Conversions tools.
    /// </summary>
    /// <author>Anders Bech Mellson, anbh@itu.dk</author>
    [PexClass]
    partial class Convertor
    {
        /// <summary>
        /// Converts a Cellular Automaton to a State[,] grid.
        /// </summary>
        [PexMethod]
        public static State[,] CAToStateGrid(CellularAutomaton ca) ]]></source>
</chunk>
<method name="CAToStateGrid" namespace="AutomaTones.Model.Tools" type="AutomaTones.Model.Tools.Convertor" typeName="Convertor" token="60001c8" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
<hit index="3" value="1"/>
<hit index="4" value="2"/>
<hit index="5" value="1"/>
<hit index="7" value="1"/>
<hit index="8" value="1"/>
<hit index="10" value="9"/>
<hit index="11" value="9"/>
<hit index="12" value="9"/>
<hit index="13" value="9"/>
<hit index="14" value="9"/>
<hit index="15" value="9"/>
<hit index="16" value="9"/>
<hit index="17" value="9"/>
<hit index="18" value="9"/>
<hit index="19" value="9"/>
<hit index="20" value="9"/>
<hit index="21" value="18"/>
<hit index="22" value="9"/>
<hit index="23" value="9"/>
<hit index="24" value="9"/>
<hit index="25" value="18"/>
<hit index="26" value="9"/>
<hit index="27" value="9"/>
<hit index="28" value="9"/>
<hit index="29" value="9"/>
<hit index="30" value="3"/>
<hit index="31" value="1"/>
<hit index="32" value="9"/>
<hit index="33" value="3"/>
<hit index="34" value="1"/>
<hit index="35" value="1"/>
<hit index="36" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="8" total="8" coveredRatio="1"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(ca!=null);
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[uint size = ca.DimensionSize;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="3" offset="0008" domain="userortestcode"><source><![CDATA[State[,] stateGrid = new State[size,size];]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            for (]]></source>
</chunk>
<chunk hits="2" offset="0012" domain="userortestcode"><source><![CDATA[int x = 0;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="5" offset="0055" domain="userortestcode"><source><![CDATA[x < size]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[; ]]></source>
</chunk>
<chunk hits="3" offset="0051" domain="userortestcode"><source><![CDATA[x++]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[)
                for (]]></source>
</chunk>
<chunk hits="3" offset="0016" domain="userortestcode"><source><![CDATA[int y = 0;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="30" offset="0045" domain="userortestcode"><source><![CDATA[y < size]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[; ]]></source>
</chunk>
<chunk hits="27" offset="0041" domain="userortestcode"><source><![CDATA[y++]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[)
                    ]]></source>
</chunk>
<chunk hits="9" offset="001a" domain="userortestcode"><source><![CDATA[stateGrid[x, y] = ca.GetCell(x,y).State;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="1" offset="0061" domain="userortestcode"><source><![CDATA[return stateGrid;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="1" offset="0066" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
    }
}
]]></source>
</chunk>
</document>
<document src="C:\Users\Anders\Documents\automatones\Visual Studio\AutomaTones\AutomaTones\Model\Automaton Model\Cellular Automaton.cs" renderPrefix="s1"><chunk hits="-1" domain="unknown"><source><![CDATA[using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Microsoft.Pex.Framework;

namespace AutomaTones.Model.Automaton_Model {
    /// <summary>
    ///   A cellular automaton consisting of an N-dimensional grid of cells.
    ///   Every new generation each cell gets a given state defined by one or more rules.
    ///   These rules consider a predefined set of neighbouring cells and hence decide a cells new state.
    ///   States are always represented using integers (discrete values).
    ///   States can be set and read via an indexer, eg. ca[0, 1] = 1;<br></br><br></br>
    ///   NOTE: Ideally this would be implemented using generics so states could also be decimals.
    ///   But due to C#'s lack of type constraint options on value types - this was omitted.<br></br><br></br>
    ///   This implementation is designed to allow a high degree of flexibility in the use of cellular automata.
    ///   Its N-dimensional and each cell may have its own state space and rule(s). Rules can use different
    ///   neighbourhoods which they process with their RuleDelegate - which again may use other delegates.
    ///   Thus this flexibility may make the overall semantics a little confusing at first.
    ///   However, one can setup a simple CA where all cells use the same space, rule and neighbourhood 
    ///   by simply using one of the constructors.
    /// </summary>
    /// <author>Anders Øland / anoe@itu.dk</author>
    [PexClass]
    [Serializable]
    public class CellularAutomaton : ICloneable {
        public uint Dimensions { get; private set; }
        public uint DimensionSize { get; private set; }
        public uint Generation { get; private set; }

        // _grid hold the cells of CA's the N-dimensional grid
        protected Cell[] Grid;

        public Rule DefaultRule { get; private set; }

        /// <summary>
        ///   Construct a new Cellular Automaton
        /// </summary>
        /// <param name = "dimensions">Number of dimensions</param>
        /// <param name = "dimensionSize">Size of the dimensions</param>
        /// <param name = "defaultRule">The default rule for all cells</param>
        /// <param name = "stateMin">default = 0</param>
        /// <param name = "stateMax">default = 1</param>
        /// <param name = "stateInit"></param>
//        [PexMethod]
        ]]></source>
</chunk>
<method name=".ctor" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" typeName="CellularAutomaton" token="600013a" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="3" coveredRatio="0"/>
</method>
<chunk hits="0" offset="0000" domain="userortestcode"><source><![CDATA[public CellularAutomaton(uint dimensions, uint dimensionSize, Rule defaultRule, int stateMin = 0,
                                 int stateMax = 1, int stateInit = 0)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="0007" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(0 < dimensions);
            Contract.Requires(0 < dimensionSize);
            Contract.Requires(null != defaultRule);
            Contract.Requires(dimensions == defaultRule.Dimensions);
            Contract.Requires(stateMin < stateMax);
            Contract.Requires(stateMin <= stateInit && stateMax >= stateInit);
            ]]></source>
</chunk>
<chunk hits="0" offset="0008" domain="userortestcode"><source><![CDATA[Init(dimensions, dimensionSize, defaultRule, stateMin, stateMax, stateInit);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="0" offset="0018" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        private void Init(uint dimensions, uint dimensionSize, Rule defaultRule,
                          int stateMin = 0, int stateMax = 1, int stateInit = 0) ]]></source>
</chunk>
<method name="Init" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" typeName="CellularAutomaton" token="600013b" module="AutomaTones.exe"><hits/>
<tags><tag offset="002a">call to external method Math.Pow(Double, Double)</tag>
<tag offset="0030">call to uninstrumented method __LimitationsHelper.floating_point_conversion(), call to uninstrumented method __LimitationsHelper.floating_point_multiplication()</tag>
</tags>
<coverage domain="userortestcode" unit="block" covered="0" total="11" coveredRatio="0"/>
</method>
<chunk hits="0" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(0 < dimensions && 0 < dimensionSize);
            Contract.Requires(dimensions == defaultRule.Dimensions);
            Contract.Requires(stateMin < stateMax);
            Contract.Requires(stateMin <= stateInit && stateMax >= stateInit);
            ]]></source>
</chunk>
<chunk hits="0" offset="0001" domain="userortestcode"><source><![CDATA[Generation = 0;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0009" domain="userortestcode"><source><![CDATA[Dimensions = dimensions;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0011" domain="userortestcode"><source><![CDATA[DimensionSize = dimensionSize;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0019" domain="userortestcode" tag="true"><tooltip><![CDATA[call to external method Math.Pow(Double, Double), call to uninstrumented method __LimitationsHelper.floating_point_conversion(), call to uninstrumented method __LimitationsHelper.floating_point_multiplication()]]></tooltip>
<source><![CDATA[Grid = new Cell[(int)Math.Pow(DimensionSize, Dimensions)];]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            for (]]></source>
</chunk>
<chunk hits="0" offset="003a" domain="userortestcode"><source><![CDATA[int i = 0;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="0058" domain="userortestcode"><source><![CDATA[i < Grid.Length]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[; ]]></source>
</chunk>
<chunk hits="0" offset="0054" domain="userortestcode"><source><![CDATA[i++]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[) ]]></source>
</chunk>
<chunk hits="0" offset="003e" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="003f" domain="userortestcode"><source><![CDATA[Grid[i] = new Cell(defaultRule, stateMin, stateMax, stateInit);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0053" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0067" domain="userortestcode"><source><![CDATA[DefaultRule = defaultRule;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="0" offset="006f" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        ///   Finds the right cell in the N-dimensional grid in the one dimensional array holding the cells
        /// </summary>
        [PexMethod]
        [Pure] // Tagged as pure since its being called from other contracts which I dont want to loosen 
        protected internal virtual Cell GetCell(params int[] coord) ]]></source>
</chunk>
<method name="GetCell" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" typeName="CellularAutomaton" token="600013c" module="AutomaTones.exe"><hits><hit index="0" value="9"/>
<hit index="1" value="9"/>
<hit index="2" value="9"/>
<hit index="3" value="9"/>
<hit index="4" value="9"/>
<hit index="5" value="9"/>
<hit index="7" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="2" total="2" coveredRatio="1"/>
</method>
<chunk hits="9" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(null != coord);
            Contract.Requires(coord.Length == Dimensions);
            Contract.Ensures(Contract.Result<Cell>() != null);
            ]]></source>
</chunk>
<chunk hits="9" offset="0001" domain="userortestcode"><source><![CDATA[uint index = GetIndex(coord);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="9" offset="0009" domain="userortestcode"><source><![CDATA[return Grid[index];]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="18" offset="0015" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        ///   Translates a coordinate into an index number pointing to the corresponing location in the grid array.
        ///   Coordinates exceeding the bounds of the grid are 'wrapped around', 
        ///   thus giving the grid a so-called torus shape.
        /// </summary>
        [Pure]
        protected uint GetIndex(int[] coord) ]]></source>
</chunk>
<method name="GetIndex" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" typeName="CellularAutomaton" token="600013d" module="AutomaTones.exe"><hits><hit index="0" value="9"/>
<hit index="1" value="3"/>
<hit index="2" value="3"/>
<hit index="3" value="3"/>
<hit index="4" value="3"/>
<hit index="5" value="3"/>
<hit index="6" value="3"/>
<hit index="7" value="2"/>
<hit index="8" value="2"/>
<hit index="9" value="2"/>
<hit index="10" value="2"/>
<hit index="11" value="2"/>
<hit index="15" value="2"/>
<hit index="16" value="2"/>
<hit index="17" value="2"/>
<hit index="18" value="2"/>
<hit index="19" value="2"/>
<hit index="20" value="2"/>
<hit index="21" value="9"/>
<hit index="22" value="9"/>
<hit index="23" value="9"/>
<hit index="25" value="2"/>
<hit index="26" value="3"/>
<hit index="28" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="11" total="14" coveredRatio="0.7857142857142857"/>
</method>
<chunk hits="9" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(null != coord);
            Contract.Requires(0 < DimensionSize && 0 < Dimensions);
            Contract.Requires(coord.Length == Dimensions);
            Contract.Ensures((uint)Math.Pow(DimensionSize, Dimensions) > Contract.Result<uint>());
            ]]></source>
</chunk>
<chunk hits="9" offset="0001" domain="userortestcode"><source><![CDATA[var index = 0;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            for (]]></source>
</chunk>
<chunk hits="9" offset="0003" domain="userortestcode"><source><![CDATA[int i = 1;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="11" offset="0060" domain="userortestcode"><source><![CDATA[i <= Dimensions]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[; ]]></source>
</chunk>
<chunk hits="2" offset="005c" domain="userortestcode"><source><![CDATA[i++]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[) ]]></source>
</chunk>
<chunk hits="3" offset="0007" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                // Loop invariant
                ]]></source>
</chunk>
<chunk hits="3" offset="0008" domain="userortestcode"><source><![CDATA[Contract.Assert(0 <= index && Grid.Length > index);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                // Make sure coordinates which exceed the bounds of any dimension are 'wrapped around'
                ]]></source>
</chunk>
<chunk hits="3" offset="0020" domain="userortestcode"><source><![CDATA[int c = coord[i - 1] % (int)DimensionSize;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="4" offset="002d" domain="userortestcode"><source><![CDATA[c = 0 <= c ? c : c + (int)DimensionSize;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                // Sum the the effect of each coordinate to find the correct position in the grid array
                ]]></source>
</chunk>
<chunk hits="2" offset="003d" domain="userortestcode"><source><![CDATA[index += c * (int)Math.Pow(DimensionSize, Dimensions - i);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="2" offset="005b" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="9" offset="0074" domain="userortestcode"><source><![CDATA[return (uint)index;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="9" offset="0078" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        ///   Translates an index number into a coordinate.
        /// </summary>
        /// <param name = "index"></param>
        /// <returns></returns>
        private int[] GetCoordinate(uint index) {
            Contract.Requires(index < Grid.Length);
            Contract.Ensures(Dimensions == Contract.Result<int[]>().Length);
            // As long as the grid is rectangular (not jagged) this is equivalent to converting
            // index from base 10 to base DimensionSize.
            return Tools.Math.DecimalToBase(index, DimensionSize, Dimensions).Reverse().ToArray();
        }

        /// <summary>
        ///   Returns the State Object of the cell.
        /// </summary>
        /// <param name = "coord"></param>
        /// <returns>The state of the cell with the given coordinates</returns>
        public State GetCellState(params int[] coord) {
            Contract.Requires(coord.Length == Dimensions);
            Contract.Ensures(Contract.Result<State>() != null);
            return GetCell(coord).State;
        }

        /// <summary>
        ///   Returns the rule of the cell with the given coordinates.
        ///   Note that one rule may consist of many rules.
        /// </summary>
        /// <param name = "coord">Coordinates</param>
        /// <returns>IRule</returns>
        [PexMethod]
        public Rule GetCellRule(params int[] coord) {
            Contract.Requires(coord.Length == Dimensions);
            Contract.Ensures(Contract.Result<Rule>() != null);
            return GetCell(coord).Rule;
        }

        /// <summary>
        ///   Sets the rule of the cell with the given coordinates.
        /// </summary>
        /// <param name = "rule"></param>
        /// <param name = "coord">Coordinates</param>
        public void SetCellRule(Rule rule, params int[] coord) {
            Contract.Requires(coord.Length == Dimensions);
            Contract.Requires(rule != null);
            GetCell(coord).Rule = rule;
        }

        /// <summary>
        ///   Add a trigger to the cell with the given coordinates.
        /// </summary>
        /// <param name = "trigger">Trigger</param>
        /// <param name = "coord">int[]</param>
        public void AddTrigger(Trigger trigger, params int[] coord) {
            Contract.Requires(coord.Length == Dimensions);
            Contract.Requires(trigger != null);
            Contract.Ensures(GetCell(coord).Triggers.Contains(trigger));
            GetCell(coord).Triggers.Add(trigger);
        }

        /// <summary>
        ///   Removes the trigger from the cell with the given coodinates.
        /// </summary>
        /// <param name = "trigger">Trigger</param>
        /// <param name = "coord">coordinates</param>
        public void RemoveTrigger(Trigger trigger, params int[] coord) {
            Contract.Requires(coord.Length == Dimensions);
            Contract.Requires(trigger != null);
            Contract.Ensures(!GetCell(coord).Triggers.Contains(trigger));
            GetCell(coord).Triggers.Remove(trigger);
        }

        /// <summary>
        ///   Evolve this Cellular Automaton - move one generation forward
        ///   by applying the rule(s) to the cells.
        /// </summary>
        public virtual void Evolve() {
            Contract.Ensures(Generation == Contract.OldValue(Generation) + 1);
            Contract.Ensures(Contract.ForAll(0, Grid.Length,
                                             i => Grid[i].State.CurrentState == Grid[i].State.NextState));
            // Calculate new states from current states
            for (uint index = 0; index < Grid.Length; index++)
                Grid[index].State.NextState = Grid[index].Rule.ApplyRule(GetCoordinate(index), this);

            // Commit new states
            foreach (Cell cell in Grid) cell.State.CurrentState = cell.State.NextState;
            Generation++;
        }

        /// <summary>
        ///   Indexer for the CA's grid of cells.
        /// </summary>
        /// <param name = "coord">coordinate</param>
        /// <returns>Cell at the given coordinate</returns>
        public virtual int this[params int[] coord] {
            get {
                Contract.Requires(coord.Length == Dimensions);
                return GetCell(coord).State.CurrentState;
            }
            set {
                Contract.Requires(coord.Length == Dimensions);
                Contract.Ensures(GetCell(coord).State.CurrentState.Equals(value));
                Grid[GetIndex(coord)].State.CurrentState = value;
            }
        }

        /// <summary>
        ///   For iterating over the entire grid.
        /// </summary>
        /// <returns></returns>
        [Pure]
        public IEnumerable<int> GridEnumerator() {
            return Grid.Select(cell => cell.State.CurrentState);
        }

        /// <summary>
        /// Returns a copy of the cellular automatons current grid of cells.
        /// </summary>
        /// <returns></returns>
        public Cell[] CopyOfGrid() {
            Contract.Ensures(Contract.ForAll(0, Grid.Length, i => Grid[i] == Contract.Result<Cell[]>()[i]));
            Cell[] copy = new Cell[Grid.Length];
            for (int i = 0; i < Grid.Length; i++) {
                Cell c = Grid[i];
                copy[i] = new Cell(c.Rule, c.State.SpaceMin, c.State.SpaceMax, c.State.CurrentState);
            }
            return copy;
        }

        [ContractInvariantMethod]
        // ReSharper disable UnusedMember.Local
        private void ObjInvar() {
            // ReSharper restore UnusedMember.Local
            Contract.Invariant(0 <= Generation);
            Contract.Invariant(0 < DimensionSize);
            Contract.Invariant(0 < Dimensions);
            Contract.Invariant(Grid.Length == (int)Math.Pow((double)DimensionSize, (double)Dimensions));
            Contract.Invariant(Contract.ForAll(Grid, c => null != c));
            Contract.Invariant(null != DefaultRule);
        }

        public override
            string ToString() {
            String s = "", t = "";
            foreach (int i in GridEnumerator()) {
                s += i;
                if (s.Length < 1 * DimensionSize) continue;
                t += s + "\n";
                s = "";
            }
            return t;
        }

        public object Clone() {
            return new CellularAutomaton(Dimensions, DimensionSize, DefaultRule) {
                Generation = Generation,
                Grid = CopyOfGrid()
            };
        }
    }

    /// <author>Anders Øland / anoe@itu.dk</author>
    /// <summary>
    /// A specialized Cellular Automaton for Wolfram one-dimensional automata.
    /// The grid does not have a torus shape (coordinates are not 'wrapper around'), instead the actual size of the grid is in fact
    /// larger than the specified size - so borderline cells can be processed.
    /// </summary>
    public class WolframAutomaton : CellularAutomaton {
        public uint ViewedSize { get; private set; }
        private readonly Cell[,] _viewedGrid;

        /// <summary>
        /// Create a new WolframAutomaton.
        /// </summary>
        /// <param name="wolframRule"></param>
        /// <param name="stateSpace">size of the state space, 2 = binary</param>
        /// <param name="viewedSize">size of the rectangular grid displaying the CA</param>
        /// <param name="actualSize">actual size of the CA in memmory</param>
        public WolframAutomaton(Rule wolframRule, int stateSpace, uint viewedSize = 64u, uint actualSize = 100u)
            : base(1, actualSize, wolframRule, 0, stateSpace - 1) {
            Contract.Requires(3 <= viewedSize);
            Contract.Requires(viewedSize <= actualSize);
            Contract.Requires(null != wolframRule);
            Contract.Requires(1 < stateSpace);
            ViewedSize = viewedSize;
            _viewedGrid = new Cell[ViewedSize, ViewedSize];
            base.GetCell((int)(actualSize / 2)).State.CurrentState = 1;
            UpdateViewedGrid();
            for (int i = 0; i < viewedSize; i++) Evolve();
        }

        /// <summary>
        ///   Returns a cell from the viewed two-dimensional grid.
        /// </summary>
        protected internal new Cell GetCell(params int[] coord) {
            Contract.Requires(coord.Length == 2);
            Contract.Ensures(Contract.Result<Cell>() != null);
            return _viewedGrid[coord[0], coord[1]];
        }

        public override sealed void Evolve() {
            base.Evolve();
            UpdateViewedGrid();
        }

        private void UpdateViewedGrid() {
            Contract.Requires(ViewedSize <= DimensionSize);
            Contract.Requires(null != _viewedGrid);
            Contract.Requires(null != Grid);
            for (int y = 0; y < ViewedSize; y++) {
                // Shift left
                for (int x = 0; x < ViewedSize - 1; x++) _viewedGrid[x, y] = _viewedGrid[x + 1, y];
                // Add new generation
                _viewedGrid[ViewedSize - 1, y] = base.GetCell((int)(ViewedSize / 2 - y + (int)DimensionSize / 2));
            }
        }
    }
}]]></source>
</chunk>
</document>
<document src="C:\Users\Anders\Documents\automatones\Visual Studio\AutomaTones\AutomaTones\Model\Automaton Model\Cell.cs" renderPrefix="s2"><chunk hits="-1" domain="unknown"><source><![CDATA[using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Microsoft.Pex.Framework;

namespace AutomaTones.Model.Automaton_Model {
    /// <summary>
    /// A cell with a given state decided by one or more rules.
    /// Multiple triggers can be attached to react to different states - by publishing events.
    /// </summary>
    /// <author>Anders Øland / anoe@itu.dk</author>
    [Serializable]
    public class Cell {
        /// <summary>
        /// The state of the cell.
        /// </summary>
        public State State { get; protected internal set; }
        
        /// <summary>
        /// The rule governing the state & evolution of this cell
        /// </summary>
        public Rule Rule { get; protected internal set; }
        
        ]]></source>
</chunk>
<method name=".ctor" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.Cell" typeName="Cell" token="6000009" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="6" coveredRatio="0"/>
</method>
<chunk hits="0" offset="0000" domain="userortestcode"><source><![CDATA[private HashSet<Trigger> _triggers = new HashSet<Trigger>();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// The set of triggers for this cell. May be empty.
        /// </summary>
        public HashSet<Trigger> Triggers {
            get { return _triggers; }
            protected internal set {
                Contract.Requires(null != value);
                Contract.Requires(Contract.ForAll<Trigger>(value, v => null != v));
                Contract.Ensures(Triggers == value);
                _triggers = value;
            }
        }

        /// <summary>
        /// Add a trigger to this cell.
        /// </summary>
        /// <param name="trigger"></param>
        public void AddTrigger(Trigger trigger) {
            Contract.Requires(null != trigger);
            Contract.Ensures(Triggers.Contains(trigger));
            _triggers.Add(trigger);
        }

        /// <summary>
        /// Remove a trigger from this cell.
        /// </summary>
        /// <param name="trigger"></param>
        public void RemoveTrigger(Trigger trigger) {
            Contract.Requires(null != trigger);
            Contract.Ensures(!Triggers.Contains(trigger));
            _triggers.Remove(trigger);
        }

        ]]></source>
</chunk>
<chunk hits="0" offset="000b" domain="userortestcode"><source><![CDATA[public Cell(Rule rule, int stateMin = 0, int stateMax = 1, int stateInit = 0)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="0012" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(null != rule);
            Contract.Requires(stateMin < stateMax);
            Contract.Requires(stateMin <= stateInit && stateMax >= stateInit);
            Contract.Ensures(Rule.Equals(rule));
            Contract.Ensures(State.SpaceMin == stateMin && State.SpaceMax == stateMax && State.CurrentState == stateInit);
            ]]></source>
</chunk>
<chunk hits="0" offset="0013" domain="userortestcode"><source><![CDATA[Rule = rule;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="001b" domain="userortestcode"><source><![CDATA[State = new State(stateMin, stateMax, stateInit);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="0" offset="002b" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        [ContractInvariantMethod]
        private void ObjInvariant() {
            Contract.Invariant(null != State);
            Contract.Invariant(null != Rule);
        }
    }
}
]]></source>
</chunk>
</document>
<document src="C:\Users\Anders\Documents\automatones\Visual Studio\AutomaTones\AutomaTones\Model\Automaton Model\State.cs" renderPrefix="s3"><chunk hits="-1" domain="unknown"><source><![CDATA[using System;
using System.Diagnostics.Contracts;

namespace AutomaTones.Model.Automaton_Model {
    /// <author>Anders Øland / anoe@itu.dk</author>
    /// <summary>
    /// The state of a cell represented by a discrete value within a given state space.. 
    /// NOTE: Ideally this would be implemented using generics so states could also be decimals.
    /// NOTE: But due to C#'s lack of type constraint options on value types - this was omitted.
    /// </summary>
    [Serializable]
    public class State {
        /// <summary>
        /// The lower boundary of the state space.
        /// </summary>
        public int SpaceMin { get; protected set; }

        /// <summary>
        /// The upper boundary of the state space.
        /// </summary>
        public int SpaceMax { get; protected set; }

        private int _currentState;

        /// <summary>
        /// Current state.
        /// </summary>
        public int CurrentState {
            get { return _currentState; }
            protected internal set {
                Contract.Ensures(CurrentState == value);
                _currentState = value;
            }
        }

        private int _nextState;

        /// <summary>
        /// Next state.
        /// </summary>
        public int NextState {
            get { return _nextState; }
            protected internal set {
                Contract.Ensures(NextState == value);
                _nextState = value;
            }
        }

        /// <summary>
        /// Construct a new State. 
        /// </summary>
        /// <param name="spaceMin"></param>
        /// <param name="spaceMax"></param>
        /// <param name="currentState"></param>
        ]]></source>
</chunk>
<method name=".ctor" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.State" typeName="State" token="60001d2" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="4" coveredRatio="0"/>
</method>
<chunk hits="0" offset="0000" domain="userortestcode"><source><![CDATA[public State(int spaceMin = 0, int spaceMax = 1, int currentState = 0)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="0007" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0008" domain="userortestcode"><source><![CDATA[SpaceMin = spaceMin;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0010" domain="userortestcode"><source><![CDATA[SpaceMax = spaceMax;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0018" domain="userortestcode"><source><![CDATA[_currentState = currentState;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="0" offset="001f" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        [ContractInvariantMethod]
        private void ObjInvariant() {
            Contract.Invariant(SpaceMin < SpaceMax);
            Contract.Invariant(SpaceMin <= _currentState && SpaceMax >= _currentState);
            Contract.Invariant(SpaceMin <= _nextState && SpaceMax >= _nextState);
        }
    }
}]]></source>
</chunk>
</document>
<document src="(no sources available for some parts of AutomaTones, AutomaTones.Explorables)" missing="true"><method name="get_DimensionSize" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" token="6000134" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<method name="Create" type="AutomaTones.Model.Automaton_Model.CellularAutomatonFactory" token="6000006" module="&lt;In Memory Module&gt;"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="2" coveredRatio="0"/>
</method>
<method name="set_Generation" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" token="6000137" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="set_Dimensions" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" token="6000133" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="set_DimensionSize" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" token="6000135" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="get_Dimensions" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" token="6000132" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<method name="set_Rule" type="AutomaTones.Model.Automaton_Model.Cell" token="6000004" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="set_SpaceMin" type="AutomaTones.Model.Automaton_Model.State" token="60001cb" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="set_SpaceMax" type="AutomaTones.Model.Automaton_Model.State" token="60001cd" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="set_State" type="AutomaTones.Model.Automaton_Model.Cell" token="6000002" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="set_DefaultRule" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" token="6000139" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="get_State" type="AutomaTones.Model.Automaton_Model.Cell" token="6000001" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
</document>
</documents>
